package cn.edu.zjicm.wordsnet_d.fragment;

import java.util.ArrayList;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.DataToRestActivity;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.activity.RegisterActivity;
import cn.edu.zjicm.wordsnet_d.activity.ShowRegularActivity;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.synchronize.PunchInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.view.CalendarView.State;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RestFragment extends Fragment implements OnClickListener {
	DataToRestActivity mDataToRestActivity;
	TextView studyTimeText, reviewNumText, newNumText, nowStateText;// ,
																	// experienceText;
	Button relaxBtn, continueBtn;
	RelativeLayout experienceNumLayout;
	ImageView showExperienceNumRegular;
	TextView experienceText;

	RelativeLayout newStudyLayout;
	TextView newStudyDivider;
	TextView finishTv;
	private View mView;
	ExamRunActivity mExamRunActivity;
	ImageView restImage;

	int mode;

	public RestFragment(){
		
	}
	
	public RestFragment(int mode) {
		this.mode = mode;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_rest, null);
		mExamRunActivity = (ExamRunActivity) getActivity();
		findView();
		initView();
		return mView;
	}

	public void initView() {
		experienceText.setText(mDataToRestActivity.getExpChange() + "");
		if (mDataToRestActivity.isfinish()) {
			AppPreference.setFirstTimeStudyNewBook(getActivity(), false);
			// if (mode == ExamRunActivity.STUDY_MODE) {
			studyTimeText.setText(getResources().getString(R.string.rest_congratulate));
			if (WordFactory.getInstance(mExamRunActivity).todayHasPunchOut()) {
				relaxBtn.setText("完成");
				finishTv.setVisibility(View.GONE);
//				relaxBtn.setVisibility(View.INVISIBLE);
			} else {
				relaxBtn.setText("打卡");
				MobclickAgentData.punchShow(getActivity());
				finishTv.setVisibility(View.VISIBLE);
			}
			// } else if (mode == ExamRunActivity.REVIEW_MODE) {
			// studyTimeText.setText(getResources().getString(R.string.rest_congratulate_review));
			// relaxBtn.setText("完成");
			// }

			studyTimeText.setTextColor(getResources().getColor(R.color.rest_end_color));
			experienceNumLayout.setVisibility(View.VISIBLE);
			relaxState();
			nowStateText.setVisibility(View.GONE);
			continueBtn.setVisibility(View.GONE);
			restImage.setImageResource(R.drawable.rest3);
		} else {
			finishTv.setVisibility(View.GONE);
			long learnTime = mDataToRestActivity.getLearnTime();
			int studySeconds = (int) (learnTime / 1000);
			int studyMinutes = studySeconds / 60;
			if (studyMinutes > 0) {
				studySeconds = studySeconds % (studyMinutes * 60);
			}

			String studyTime;
			if (studyMinutes == 0) {
				studyTime = studySeconds + "秒";
			} else {
				studyTime = studyMinutes + "分" + studySeconds + "秒";
			}

			switch (studyMinutes) {
			case 20:
				studyTimeText.setText(getResources().getString(R.string.rest_20m));
				nowStateText.setText(getResources().getString(R.string.rest_state2));
				relaxState();
				break;
			case 40:
				studyTimeText.setText(getResources().getString(R.string.rest_40m));
				nowStateText.setText(getResources().getString(R.string.rest_state3));
				relaxState();
				break;
			case 60:
				studyTimeText.setText(getResources().getString(R.string.rest_60m));
				nowStateText.setText(getResources().getString(R.string.rest_state4));
				relaxState();
				break;
			case 80:
				studyTimeText.setText(getResources().getString(R.string.rest_80m));
				nowStateText.setText(getResources().getString(R.string.rest_state5));
				relaxState();
				break;
			default:
				studyTimeText.setText("您刚才学习了" + studyTime);
				if (studyMinutes < 20) {
					continueState();
				} else {
					relaxState();
				}
				nowStateText.setVisibility(View.GONE);
				break;
			}
		}
		reviewNumText.setText("" + mDataToRestActivity.getReviewNum());
		if (mode == ExamRunActivity.STUDY_MODE) {
			newNumText.setText("" + mDataToRestActivity.getLearnNum());
		} else {
			newStudyLayout.setVisibility(View.GONE);
			newStudyDivider.setVisibility(View.GONE);
		}

		relaxBtn.setOnClickListener(this);
		continueBtn.setOnClickListener(this);
		finishTv.setOnClickListener(this);
		showExperienceNumRegular.setOnClickListener(this);
	}

	// 该休息时
	private void relaxState() {
		nowStateText.setVisibility(View.VISIBLE);
		restImage.setImageResource(R.drawable.rest2);
		relaxBtn.setFocusable(true);
		continueBtn.setFocusable(false);
		relaxBtn.setFocusableInTouchMode(true);
		continueBtn.setFocusableInTouchMode(false);
		relaxBtn.requestFocus();
	}

	// 该学习时
	private void continueState() {
		restImage.setImageResource(R.drawable.rest1);
		relaxBtn.setFocusable(false);
		continueBtn.setFocusable(true);
		relaxBtn.setFocusableInTouchMode(false);
		continueBtn.setFocusableInTouchMode(true);
		continueBtn.requestFocus();
	}

	private void findView() {
		studyTimeText = (TextView) mView.findViewById(R.id.rest_studyTimeText);
		reviewNumText = (TextView) mView.findViewById(R.id.rest_reviewNumText);
		newNumText = (TextView) mView.findViewById(R.id.rest_newNumText);
		nowStateText = (TextView) mView.findViewById(R.id.rest_nowStateText);
		relaxBtn = (Button) mView.findViewById(R.id.rest_relaxBtn);
		continueBtn = (Button) mView.findViewById(R.id.rest_continueBtn);
		mDataToRestActivity = DataToRestActivity.getInstance();
		restImage = (ImageView) mView.findViewById(R.id.rest_image);
		experienceNumLayout = (RelativeLayout) mView.findViewById(R.id.rest_experienceNumLayout);
		experienceText = (TextView) mView.findViewById(R.id.rest_experienceNumTv);
		showExperienceNumRegular = (ImageView) mView.findViewById(R.id.experienceNumIv);
		newStudyLayout = (RelativeLayout) mView.findViewById(R.id.new_study_layout);
		newStudyDivider = (TextView) mView.findViewById(R.id.new_study_divider);

		finishTv = (TextView) mView.findViewById(R.id.finishTv);
		// SpannableString spannableString = new
		// SpannableString(finishTv.getText());
		// spannableString.setSpan(new UnderlineSpan(), 0,
		// spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		// finishTv.setText(spannableString);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rest_relaxBtn:
			if (mDataToRestActivity.isfinish()) {
				// if (mode == ExamRunActivity.REVIEW_MODE) {
				// mExamRunActivity.toFinish();
				// } else if (mode == ExamRunActivity.STUDY_MODE) {
				if (WordFactory.getInstance(mExamRunActivity).todayHasPunchOut()) {
					mExamRunActivity.toFinish();
				} else {
					MobclickAgentData.punchClick(getActivity());
					if (UserInterface.getInstance(mExamRunActivity).isLogin()) {
						punch();
					} else {
						// LoginActivity.startActivity(mExamRunActivity, false,
						// LoginActivity.TO_PUNCH_OUT);
						RegisterActivity.startActivity(mExamRunActivity, RegisterActivity.TO_PUNCH_OUT);
						MobclickAgentData.punchFail(getActivity(), "未登录");
					}
				}
				// }

			} else {
				mExamRunActivity.toFinish();
			}
			break;

		case R.id.rest_continueBtn:
			mExamRunActivity.continueStudy();
			break;
		case R.id.experienceNumIv:
			ShowRegularActivity.startActivity(mExamRunActivity, ShowRegularActivity.TYPE_EXPERIENCE_NUM);
			break;
		case R.id.finishTv:
			LogUtil.d("点击完成记录时间");
			recordNotPunchDate();
			mExamRunActivity.toFinish();
			break;
		}
	}
	
	/**
	 * 记录没有打卡的日期
	 */
	public void recordNotPunchDate(){
		int date = DateTimeUtils.getDate();
		AppPreference.addNoPunchDate(mExamRunActivity, date+"");
	}

	protected void punch() {
//		MobclickAgentData.share(getActivity(), "打卡成功");
		punchHandler.sendMessage(punchHandler.obtainMessage(UserInterface.MSG_START));
		PunchInterface.getInstance(getActivity()).punch(punchHandler,State.TODAY);
	}

	private Handler punchHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}

			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(getActivity(), null, "正在打卡...");
				break;
			case UserInterface.MSG_SUCCESS:
				mExamRunActivity.showPunchOutFragment();
				MobclickAgentData.punchSuccess(getActivity());
				break;
			case UserInterface.MSG_UNSUCCESS:
				Toast.makeText(getActivity(), "打卡失败，一天只打一次卡", Toast.LENGTH_SHORT).show();
				MobclickAgentData.punchFail(getActivity(), "一天只能打一次卡");
				break;
			case UserInterface.MSG_BAD_NETWORK:
				Toast.makeText(getActivity(), "打卡失败，" + UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				MobclickAgentData.punchFail(getActivity(), "未检测到网络连接");
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(getActivity(), "打卡失败，" + UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				MobclickAgentData.punchFail(getActivity(), "连接服务器失败");
				break;
			default:
				break;
			}
		};
	};
}
